<?php
namespace AuctionBack\Controller;

use AuctionCore\System\Menu;
use AuctionCore\Common\Tree;

/**
 * 后台首页控制器
 * 主要获取首页聚合数据
 */
class MenuController extends BaseController {
    public function __construct() {        
        parent::__construct();
    }

    public function index()
    {
        $result = Menu::menuList();
        $tree = new Tree();
        $tree->icon = array('&nbsp;&nbsp;&nbsp;│ ', '&nbsp;&nbsp;&nbsp;├─ ', '&nbsp;&nbsp;&nbsp;└─ ');
        $tree->nbsp = '&nbsp;&nbsp;&nbsp;';

        $newmenus=array();
        foreach ($result as $m){
            $newmenus[$m['id']]=$m;
        }
        foreach ($result as $n=> $r) {
            $result[$n]['node_id'] = ($r['parent_id']>0) ? $r['parent_id']."-".$r['id'] : $r['id'] ;
            $result[$n]['level'] = $this->_get_level($r['id'], $newmenus);
            $result[$n]['parent_node'] = ($r['parent_id']) ? ' data-tt-parent-id="' . $r['parent_id'] . '"' : '';
            $result[$n]['str_manage'] = "<a href='/Menu/edit/id/{$r['id']}'>编辑</a> | <a href='#'>删除</a>" ;
            if($r['parent_id']==0){
                $result[$n]['str_manage'].= " | <a href='/Menu/add/parent_id/{$r['id']}'>添加子菜单</a>";
            }
            $result[$n]['status'] = $r['status'] ? "显示" : "隐藏";
            $result[$n]['app']= $r['model']."/".$r['action'];
            $result[$n]['type'] = $r['type']==1 ? "权限认证" : "普通菜单";

        }
        $tree->init($result);
        $str = "<tr data-tt-id='\$node_id' \$parent_node>
                    <td style='padding-left:20px;'><input name='listorders[\$id]' type='text' size='3' value='\$listorder' class='input input-order'></td>
                    <td>\$id</td>
                    <td>\$spacer\$name</td>
                    <td>\$app</td>
                    <td>\$type</td>
                    <td>\$status</td>
                    <td>\$str_manage</td>
                </tr>";
        $categorys = $tree->get_tree(0, $str);

        $this->assign("categorys", $categorys);
        $this->display();
    }

    public function add(){
        $parent_id = I("get.parent_id/d",0);
        $parents = Menu::menuList(0);
        $this->assign('parent_id',$parent_id);
        $this->assign('parents',$parents);
        $this->display();
    }

    public function saveAdd()
    {
        $parent_id = I('post.parent_id/d');
        $name = I('post.name');
        $model = I('post.model');
        $action = I('post.action');
        $extra = I('post.extra');
        $rules = I('post.rules');
        $type = I('post.type');

        $ret = Menu::create($parent_id,$name,$model,$action,$extra,$rules,$type);
        if($ret<1){
            $this->helpJsonResult(500,'添加失败');
        }
        $this->helpJsonResult(200,'添加成功');
    }

    public function edit()
    {
        $id = I("get.id/d",0);
        $detail = Menu::detailById($id);
        $parents = Menu::menuList(0);
        $this->assign("parents",$parents);
        $this->assign('detail',$detail);
        $this->display();
    }

    public function save_edit()
    {
        $id = I("post.id");
        if($id<1){
            $this->helpJsonResult("菜单id不正确");
        }
        $menu = new Menu($id);
        $parent_id = I("post.parent_id");
        $name = I("post.name");
        $model = I("post.model");
        $action = I("post.action");
        $extra = I("post.extra");
        $type = I("post.type");
        $status = I("post.status");
        $rules = I("post.rules");

        $data = array(
            'parent_id' => $parent_id,
            'name' => $name,
            'model' => $model,
            'action' => $action,
            'extra' => $extra,
            'type' => $type,
            'status' => $status,
            'rules' => $rules,
        );
        $ret = $menu->update($data);
        if(!$ret){
            $this->helpJsonResult(500,"更新失败");
        }
        $this->helpJsonResult(200,"操作成功");
    }

    /**
     * 获取菜单深度
     * @param $id
     * @param $array
     * @param $i
     * @return array
     */
    protected function _get_level($id, $array = array(), $i = 0) {

        if ($array[$id]['parent_id']==0 || empty($array[$array[$id]['parent_id']]) || $array[$id]['parent_id']==$id){
            return  $i;
        }else{
            $i++;
            return $this->_get_level($array[$id]['parent_id'],$array,$i);
        }

    }

    //排序
    public function list_orders()
    {
        $status = Menu::list_orders();
        if (!$status) {
            $this->helpJsonResult(500,'更新失败');
        }
        $this->helpJsonResult(200,'更新成功');
    }

}
